<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-10-08
 * Time: 19:22
 */

namespace App\Http\Controllers\api\traits;


use Request;
use Response;

trait ResponseController
{

    public function success($array = [])
    {
        if (is_string($array)) {
            $array = ['message' => $array];
        }
        $default = [
            'status'  => true,
            'message' => '操作成功',
        ];
        return $this->json(array_merge($default, $array));
    }

    public function failed($array = [], $reason = null)
    {
        if (is_string($array)) {
            $array = [
                'message' => $array,
                'reason'  => $reason,
            ];
        }
        $default = [
            'status'  => false,
            'message' => '操作失败',
            'reason'  => '',
        ];
        return $this->json(array_merge($default, $array));
    }

    public function package($data)
    {
        $package = [
            'status' => true,
            'type'   => 'object',
            'data'   => $data,
        ];
        return $this->json($package);
    }

    public function packageList($data)
    {
        $package = [
            'status' => true,
            'type'   => 'list',
            'data'   => $data,
        ];
        return $this->json($package);
    }

    protected function json($json)
    {
        return Response::json($json);
    }
}